package com.aier.classes.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.classes.entity.ClassPhotoType;

public class ClassPhotoTypeRowMapper implements RowMapper<ClassPhotoType> {

    @SuppressWarnings("unused")
    private int type;
    
    public static final int AND_NUM=1;
    

    public ClassPhotoTypeRowMapper() {
    }

    public ClassPhotoTypeRowMapper(int type) {
        this.type = type;
    }

    @Override
    public ClassPhotoType mapRow(ResultSet rs, int rowNum) throws SQLException {

        ClassPhotoType u = parseClassPhotoType(new ClassPhotoType(), rs);
        switch(type) {
            case AND_NUM:
                u.setNum_img(rs.getInt("num_img"));
        }
        return u;
    }

    public static ClassPhotoType parseClassPhotoType(ClassPhotoType u, ResultSet rs) throws SQLException {
        return parseClassPhotoType(u, rs, null);
    }

    public static ClassPhotoType parseClassPhotoType(ClassPhotoType u, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            u.setCptid(rs.getLong(su + "cptid"));
        } catch (SQLException e) {
            u.setCptid(null);
        }
        try {
            u.setCid(rs.getLong(su + "cid"));
        } catch (SQLException e) {
            u.setCid(null);
        }
        try {
            u.setPname(rs.getString(su + "pname"));
        } catch (SQLException e) {
            u.setPname(null);
        }
        try {
            u.setImg(rs.getString(su + "img"));
        } catch (SQLException e) {
            u.setImg(null);
        }
        try {
            u.setCdate(rs.getTimestamp(su + "cdate"));
        } catch (SQLException e) {
            u.setCdate(null);
        }        
        return u;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, ClassPhotoType u) {
        par.put("cptid", u.getCptid());
        par.put("cid", u.getCid());
        par.put("pname", u.getPname());
        par.put("img", u.getImg());
        par.put("cdate", u.getCdate());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, ClassPhotoType u) {
        args.put("cptid", u.getCptid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final ClassPhotoType u) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(u),
                        new String[] { "cptid" });
                parsePS(ps, u);
                return ps;
            }

        };
    }

    private static String parseSQL(ClassPhotoType u) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into class_photo_type(");
        int count = 0;
        if (null != u.getCid()) {
            sql.append("cid,");
            count++;
        }
        if (null != u.getPname()) {
            sql.append("pname,");
            count++;
        }
        if (null != u.getImg()) {
            sql.append("img,");
            count++;
        }
        if (null != u.getCdate()) {
            sql.append("cdate,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, ClassPhotoType u)
        throws SQLException {
        int count = 1;
        if (null != u.getCid()) {
            ps.setLong(count++, u.getCid());
        }
        if (null != u.getPname()) {
            ps.setString(count++, u.getPname());
        }
        if (null != u.getImg()) {
            ps.setString(count++, u.getImg());
        }
        if (null != u.getCdate()) {
            ps.setTimestamp(count++, u.getCdate());
        }

    }

}
